iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
1
影片教學

R語言-預測方法大全系列 第 20

[Day-20] 預測操作--xgb-中 (xgb tuning in r)

  • 分享至 

  • xImage
  •  

您的訂閱是我製作影片的動力
訂閱點這裡~/images/emoticon/emoticon39.gif

Yes

若內容有誤,還請留言指正,謝謝您的指教/images/emoticon/emoticon07.gif

#影片程式碼
#延續DAY19
importance <- xgb.importance(model = model)
par(mfrow=c(1,1))
xgb.plot.importance(importance, top_n = 3, measure = "Gain")

hyper_grid <- expand.grid(eta = c( .05, .1),
                          max_depth = c( 5, 7),
                          min_child_weight = c( 3, 5, 7),
                          subsample = c(.65, .8), 
                          colsample_bytree = c(.8, .9, 1),
                          nrounds = 0, 
                          RMSE = 0) 
nrow(hyper_grid)

for(i in 1:nrow(hyper_grid)) {
  params <- list(
    eta = hyper_grid$eta[i],
    max_depth = hyper_grid$max_depth[i],
    min_child_weight = hyper_grid$min_child_weight[i],
    subsample = hyper_grid$subsample[i],
    colsample_bytree = hyper_grid$colsample_bytree[i]
  )
  set.seed(123)
  xgb.tune <- xgb.cv(
    params = params,
    data = trainx[,-c(4:6)],
    label = trainy,
    nrounds = 500, 
    nfold = 5,
    objective = "reg:squarederror",  # binary:logistic分類
    verbose = 0,               
    early_stopping_rounds = 10 
  )
  
  hyper_grid$nrounds[i] <- which.min(xgb.tune$evaluation_log$test_rmse_mean)
  hyper_grid$RMSE[i] <- min(xgb.tune$evaluation_log$test_rmse_mean)
}
hyper_grid %>% dplyr::arrange(RMSE) %>% head(10)

上一篇
[Day-19] 預測操作--xgb-上 (gb xgb in r)
下一篇
[Day-21] 預測操作--xgb-下 (xgb predict in r)
系列文
R語言-預測方法大全30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言